package com.wgc.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wgc.entity.PageResult;
import com.wgc.entity.Result;
import com.wgc.user.domain.User;
import com.wgc.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
/**
 * 用户
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 查询全部用户表
     */
    @GetMapping("/selectAll")
    public Result<List<User>> selectAll() {
        return new Result<>("查询成功", userService.selectAll());
    }

    /**
     * 新增用户表
     *
     * @param iUser
     * @return
     */
    @GetMapping("/insert")
    public Result<Integer> insert(User iUser) {
        if ( userService.insert(iUser) == 1) {
            return new Result<>("新增成功");
        }
        else {
            return new Result<>("新增失败，用户名重复");
        }
    }

    /**
     * 上传头像
     */
    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file, @RequestParam Long id) {
        return new Result<>("上传成功");
    }
    /**
     * 修改用户表
     */
    @PostMapping("/update")
    public Result update(@RequestBody User iUser){
        userService.update(iUser);
return new Result<>("修改成功");
    }
    /**
     * 删除用户表
     * @param ids
     * @return
     */
    @GetMapping("/delete")
    public Result delete(@RequestParam String ids) {
        userService.delete(ids);
        return new Result<>("删除成功");
    }
    /**
     * 条件查询用户表
     * @param iUser
     * @return
     */
    @PostMapping("/selectList")
    public Result< List<User> > select(@RequestBody User iUser) {
        return new Result<>("查询成功", userService.selectList(iUser));
    }
    /**
     * 分页搜索实现用户表
     * @param page 默认值1
     * @param size 默认值10
     * @return
     */
    @GetMapping(value = "/pageAll" )
    public Result pageAll(@RequestParam(defaultValue = "1")  Integer page, @RequestParam(defaultValue = "10") Integer size) {
        Page<User> userPage = userService.pageAll(page, size);
        PageResult<User> pageResult = new PageResult<>(userPage.getTotal(), userPage.getRecords());
        return new Result<>("查询成功", pageResult);
    }
    /**
     * 分页条件搜索实现用户表
     * @param iUser
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/pageList" )
    public Result pageList(@RequestBody User iUser,@RequestParam(defaultValue = "1")  Integer page, @RequestParam(defaultValue = "10") Integer size) {
        Page<User> userPage = userService.pageList(iUser, page, size);
        PageResult<User> pageResult = new PageResult<>(userPage.getTotal(), userPage.getRecords());
        return new Result<>("查询成功", pageResult);
    }
    /**
     * 启动、
     * 停用
     * @param id
     * @param status
     * @return
     */
    @GetMapping("/status")
    public Result updateStatus(@RequestParam Long id,@RequestParam Integer status){
        userService.status(id,status);
        return new Result("修改成功");
    }
    /**
     * 登录
     *
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        String login = userService.login(user.getUsername(), user.getPassword());
        switch (login) {
            case "-1":
                return Result.fail("用户名或密码错误");
            case "-2":
                return Result.fail("该用户不存在");
            case "-3":
                return Result.fail("该用户已停用");
        }
        return new Result("登录成功",login);
    }

}
